<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */
 
require_once (ROOT_PATH."xajax_core/xajax.inc.php");

require_once(ROOT_PATH."class/files.class.php");
require_once(ROOT_PATH."class/status.class.php");
require_once(ROOT_PATH."class/user.class.php");
require_once(ROOT_PATH."class/history.class.php");
require_once(ROOT_PATH."class/project.class.php");
require_once(ROOT_PATH."class/team.class.php");

$xajax = new xajax("xr.php");
$xajax->registerFunction("loadHistory");
$xajax->registerFunction("sendMessage");
$xajax->registerFunction("getHelp");
$xajax->registerFunction("updateFile");
$xajax->registerFunction("workload");
$xajax->registerFunction("sendMeMyFiles");
//$xajax->setFlag("debug",true);
//$xajax->setFlag("statusMessages",true);

if (isset($param['projectid'])) {
	$_SESSION['projectid'] = $param['projectid'];
	$prj = new project();
	$prj->id=$param['projectid'];
	$loader->get($prj);
	$_SESSION['projectname'] = $prj->name;

	if (SAVE_SESSION == 'true') {
		setcookie('poat['.$CURRENT_USER->userid.'][projectid]',$_SESSION['projectid'],time()+(COOKIE_EXPIRE*60*60));
		$param['fstatusid'] = $_COOKIE['poat'][$CURRENT_USER->userid]['prj'.$_SESSION['projectid']]['fstatusid'];
		$param['fuserid'] = $_COOKIE['poat'][$CURRENT_USER->userid]['prj'.$_SESSION['projectid']]['fuserid'];		
		$param['fpath'] = $_COOKIE['poat'][$CURRENT_USER->userid]['prj'.$_SESSION['projectid']]['fpath'];
		$param['forder'] = $_COOKIE['poat'][$CURRENT_USER->userid]['prj'.$_SESSION['projectid']]['forder'];
	}
}

if (isset($param['teamid'])) {
	$currentteam = $param['teamid'];
	$tm->assign('teamid',$param['teamid']);	
} else {
	$currentteam = $CURRENT_USER->teamid;
}

if (SAVE_SESSION == 'true') {
	setcookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][fstatusid]',$param['fstatusid'],time()+(COOKIE_EXPIRE*60*60));
	setcookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][fuserid]',$param['fuserid'],time()+(COOKIE_EXPIRE*60*60));
	setcookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][fpath]',$param['fpath'],time()+(COOKIE_EXPIRE*60*60));
	setcookie('poat['.$CURRENT_USER->userid.'][prj'.$_SESSION["projectid"].'][forder]',$param['forder'],time()+(COOKIE_EXPIRE*60*60));
}

if (!isset($param['forder'])) {
	$param['forder']="asc";
} else {
	if ($param['op']=="order") {
		$param['forder'] = $param['forder']=="asc" ? "desc" : "asc"; 
	}
}

$files = new files($_SESSION['projectid']);
$files->statusid = $param['fstatusid'];
$files->userid = $param['fuserid'];
$files->path = $param['fpath'];
$files->teamid = $currentteam;
$files->order=$param['forder'];

/* IF IS FILTERED BY USER ONLY SHOW ASSIGNED FILES*/
if ($files->userid!='' and ($files->statusid!='' and ($files->statusid != FILE_STATUS_LOCKED or $files->statusid != FILE_STATUS_CMPND or $files->statusid != FILE_STATUS_PROOF))) {
	$list = array();
} else {
	if ($files->userid !='') {
		$files->statusid = array(FILE_STATUS_LOCKED,FILE_STATUS_CMPND);
	}
	$list = $loader->loadListBy($files,$files->loadfiltered());
}

debug($list);

$flist = array();

$prj = new project();
$prj->id=$_SESSION['projectid'];
$loader->get($prj);

if ($prj->statsupdated!=null) {
	$ar=array(util::fromUTC($prj->statsupdated,$CURRENT_USER->timezone));
	$pmessage = createMessage($LANG['statsupdated'],$ar);
}

$sprjlist = $loader->loadListBy($prj,$prj->listSubProjects());

$team = new team;
$team->id = $currentteam;

$loader->get($team);

$tuns = 0;
$tfuz = 0;
foreach ($list as $key => $obj) {
	$obj->lastupdate = util::fromUTC($obj->lastupdate,$CURRENT_USER->timezone);
	$found=false;
	for ($i=0;$i<count($sprjlist) and !$found;$i++) {
		if ($sprjlist[$i]->id==$obj->subprj) {
			$obj->url = VIEW_SVN_URL . $prj->path . str_replace('$LANG',$team->lang,$sprjlist[$i]->path) . "/" . $obj->name;
			$found=true;	
		}
	}
	$tuns+=$obj->untranslated;
	$tfuz+=$obj->fuzzy;
	array_push($flist,$obj);
}

$tm->assign('list',$flist);

$tm->assign('statuslist',$FILE_STATUS_LIST);
$astatus = $FILE_STATUS_LIST;

if ($CURRENT_USER->groupid == GROUP_USER) {
	$astatus = array(FILE_STATUS_CHKPND,FILE_STATUS_DONE);
}

$tm->assign('astatuslist',$astatus);

$users=array();

//if ($CURRENT_USER->groupid == GROUP_COORD) {
	$user = new user;
	$user->teamid = $currentteam;
	$userlist = $loader->loadlistBy($user,$user->listByFilters());

	foreach ($userlist as $key => $obj) {
		array_push($users,$obj->userid);
	}
	
//} else {
//	array_push($users,$CURRENT_USER->userid);	
//}


$tm->assign('userlist',$users);

$pathlist = $loader->loadListBy($files,$files->pathlist());

debug($pathlist);

$paths=array();

foreach ($pathlist as $key => $obj) {
	array_push($paths,$obj->path);
}

$tm->assign('script',$xajax->getJavascript('scripts'));
$tm->assign('projectmessage',$pmessage);
$tm->assign('totaluntranslated',$tuns);
$tm->assign('totalfuzzy',$tfuz);
$tm->assign('projectname',$_SESSION['projectname']);
$tm->assign('pathlist',$paths);
$tm->assign('order',$param['forder']);
$tm->assign('statusid',$param['fstatusid']);
$tm->assign('userid',$param['fuserid']);
$tm->assign('path',$param['fpath']);
$content = $tm->fetch('list.tpl');

?>